package com.easyCode.sqlManager;

import com.easyCode.sqlBuilder.SqlInfo;
import org.springframework.util.Assert;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;

/**
 * 负责将java类型转换到mysql类型
 * @author 大忽悠
 * @create 2022/12/8 21:21
 */
public class MappingContainer {
    /**
     * java类型到sql类型映射
     */
    protected static final Map<Class<?>, String> java_to_sql_mappings=new HashMap<>();
    /**
     * sql类型到java类型映射
     */
    protected static final Map<String,Class<?>> sql_to_java_mappings=new HashMap<>();
    /**
     * sql类型的默认长度
     */
    protected static final Map<String, String> default_len=new HashMap<>();
    /**
     * 根据sqlInfo的Class,实例化生成对应的SqlInfo实例对象
     */
    protected static final Map<Class<? extends SqlInfo>, Function<Class<? extends SqlInfo>,? extends SqlInfo>> sql_info_mappings=new HashMap<>();

    public static String getMysqlType(Class<?> javaType){
        String filedType = java_to_sql_mappings.get(javaType);
        Assert.notNull(filedType,"目前暂不支持将"+ javaType +"类型映射到当前sql某个数据类型!");
        return filedType;
    }

    public static Class<?> getJavaType(String sqlType){
        Class<?> javaClass = sql_to_java_mappings.get(sqlType);
        Assert.notNull(javaClass,"目前暂不支持将"+sqlType+"类型映射到java某个类型");
        return javaClass;
    }

    public static String getDefaultLen(Class<?> javaType) {
       String sqlType=java_to_sql_mappings.get(javaType);
       return sqlType==null ? null : default_len.get(sqlType);
    }

    public static SqlInfo getSqlInfo(Class<? extends SqlInfo> sqlInfoClass){
        Function<Class<? extends SqlInfo>, ? extends SqlInfo> function = sql_info_mappings.get(sqlInfoClass);
        Assert.notNull(function,"不支持实例"+ sqlInfoClass +"类型的对象");
        return function.apply(sqlInfoClass);
    }
}
